package com.mm.mindmap.consistency;


import com.mm.common.utils.ContextUtil;
import com.mm.mindmap.consistency.dataFunc.DataFunc;
import com.mm.mindmap.consistency.strategy.scheme.IConsistencyScheme;
import org.springframework.context.ApplicationContext;
import java.util.List;

/**
 * @Program: 8.mindmap
 * @Author: 海里的鱼
 * @Create: 2024-08-28 08:56
 * @Name: 保证数据一致性的工具类
 * @Desc: Redis，ES，MySQL的数据一致性
 */
public class ConsistencyUtil {
    //Bean容器，记得在启动类里添加
    private static ApplicationContext context = ContextUtil.getContext();

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 设置容器
     * @Describe: TODO
     **/
     public static void setContext(ApplicationContext context) {
        ConsistencyUtil.context = context;
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获得一个实现一致性的处理器
     * @Describe: TODO
     **/
     public static ConsistencyHandler getHandler(List<ConsistencyData> dataList){
        return new ConsistencyHandler(dataList);
    }

    public static ConsistencyHandler getHandler(){
        return new ConsistencyHandler();
    }



    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 创建一个写入数据ConsistencyData
     * @Describe: TODO
     **/
    public static <T1 extends DataFunc,T2 extends IConsistencyScheme> ConsistencyData getConsistencyData(Object data, DataType type, Class<T1> func, Class<T2> scheme){
        return new ConsistencyData(data,type,context.getBean(func),context.getBean(scheme));
    }
    public static <T1 extends DataFunc,T2 extends IConsistencyScheme> ConsistencyData getMysqlWrite(Object data, Class<T1> func, Class<T2> scheme){
        return new ConsistencyData(data, DataType.MYSQL,context.getBean(func), context.getBean(scheme));
    }

    public static <T1 extends DataFunc,T2 extends IConsistencyScheme> ConsistencyData getRedisWrite(Object data, Class<T1> func, Class<T2> scheme){
        return new ConsistencyData(data, DataType.REDIS,context.getBean(func),context.getBean(scheme));
    }

    public static <T1 extends DataFunc,T2 extends IConsistencyScheme> ConsistencyData getEsWrite(Object data, Class<T1> func, Class<T2> scheme){
        return new ConsistencyData(data, DataType.ELASTICSEARCH,context.getBean(func),context.getBean(scheme));
    }

}
